import 'dart:convert';
import 'dart:core';
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

import '../Gesture/GestureDetectorConflictTest.dart';
import '../Gesture/GestureDetectorHitTest.dart';
import '../Gesture/NotificationInterruptTest.dart';

///原始HttpSetting请求的使用
class HttpSetting extends StatelessWidget {
  HttpClient httpClient = new HttpClient();

  void setHttpRequestHead() async {
    Uri uri = Uri.parse(
        "https://admin.leke.cn/eb/admin/apk/checkUpdate?deviceType=HC_TEMPERATURE&versionCode=1042");
    HttpClientRequest request = await httpClient.getUrl(uri);
    // request.headers.add("token", "test");
    HttpClientResponse response = await request.close();
    String responseBody = await response.transform(utf8.decoder).join();
    print("结果：$responseBody");
    ///常规转换 方式
    Map resultMap =  json.decode(responseBody);
    var result = BaseHttpResult.fromJson(resultMap);
    print("json解析结果：${result.data?.toString()}");
  }



  @override
  Widget build(BuildContext context) {
    return Material(
      child: ElevatedButton(
        child: Text("请求更新数据"),
        onPressed: setHttpRequestHead,
      ),
    );
  }
}

class BaseHttpResult {
  int code = 0;
  String? message;
  String? time = "";
  bool? success;
  UpdateVersionBean? data;

  BaseHttpResult.fromJson(Map<dynamic, dynamic> jsonMap)
      : code = jsonMap['code'],
        time = jsonMap['time'],
        success = jsonMap['success'],
        data = UpdateVersionBean.fromJson(jsonMap['data']),
        message = jsonMap['message'];

  Map<String, dynamic> toJson() => <String, dynamic>{
        'code': code,
        'time': time,
        'success': success,
        'data': data,
        'message': message,
      };

  @override
  String toString() {
    return 'BaseHttpResult{code: $code, message: $message, time: $time, success: $success, data: $data}';
  }
}

class UpdateVersionBean {
  int versionCode = -1;
  int id = -1;
  String? versionName;
  String? updateTip;
  String? apkUrl;
  bool? isNeedUpdate;
  bool? forceUpdate;

  UpdateVersionBean.fromJson(Map<dynamic, dynamic> json)
      : versionCode = json['versionCode'],
        id = json['id'],
        versionName = json['versionName'],
        updateTip = json['updateTip'],
        apkUrl = json['apkUrl'],
        isNeedUpdate = json['isNeedUpdate'],
        forceUpdate = json['forceUpdate'];

  Map<String, dynamic> toJson() => <String, dynamic>{
        'versionCode': versionCode,
        'id': id,
        'versionName': versionName,
        'updateTip': updateTip,
        'apkUrl': apkUrl,
        'isNeedUpdate': isNeedUpdate,
        'forceUpdate': forceUpdate,
      };

  @override
  String toString() {
    return 'UpdateVersionBean{versionCode: $versionCode, id: $id, versionName: $versionName, updateTip: $updateTip, apkUrl: $apkUrl, isNeedUpdate: $isNeedUpdate, forceUpdate: $forceUpdate}';
  }
}
